package com.karson.front.controller;

import com.karson.common.exception.ExceptionCodeEnum;
import com.karson.common.request.PlayProgressRequest;
import com.karson.common.request.UserActionRequest;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommonResult;
import com.karson.common.vo.ActionStatusVo;
import com.karson.service.service.UserActionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/action")
@Tag(name = "用户行为接口")
public class UserActionController {
    @Autowired
    private UserActionService userActionService;
    @Operation(summary = "用户操作行为记录")
    @PostMapping("userAction/record")
    public CommonResult<String> record(@RequestBody UserActionRequest userAction) {
        if (userAction == null) {
            return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED);
        }
        userActionService.recordUserAction(userAction);
        return CommonResult.success();
    }

    @Operation(summary = "获取用户对专辑的行为状态")
    @GetMapping("/getActionStatus")
    public CommonResult<ActionStatusVo> getActionStatus(@RequestParam Integer albumId,@RequestParam String type) {
        if (albumId != null && albumId <= 0) {
            return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED);
        }
        ActionStatusVo actionStatusVo =  userActionService.getActionStatus(albumId,type);
        return CommonResult.success(actionStatusVo);
    }
    @Operation(summary = "用户播放行为记录")
    @PostMapping("/play")
    public CommonResult<String> reportProgress (@RequestBody @Validated PlayProgressRequest request) {
        if (request == null) {
            return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED);
        }
        userActionService.reportProgress(request.getTrackId(),request.getCurrentTime(),request.getDuration());
        return CommonResult.success("ok");
    }
    @Operation(summary = "用户播放完成/退出时记录上报")
    @PostMapping("/finalize")
    public CommonResult<String> finalize (@RequestBody @Validated PlayProgressRequest request) {
        userActionService.finalizePlayback(request.getTrackId(),request.getDuration());
        return CommonResult.success("ok");
    }


}
